package com.zadtek.shopmall.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.util.ArrayMap;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.activities.OrderActivity;
import com.zadtek.shopmall.adapter.MyAdapter;
import com.zadtek.shopmall.base.AbsListFragment;
import com.zadtek.shopmall.bean.OrderBean;
import com.zadtek.shopmall.bean.OrderProudctBean;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;
import com.zadtek.shopmall.other.Const;
import com.zadtek.shopmall.utils.ToastUtil;
import com.zadtek.shopmall.view.NoScrollView;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import static com.zadtek.shopmall.activities.RedEnvelopActivity.TAB_1;
import static com.zadtek.shopmall.activities.RedEnvelopActivity.TAB_2;
import static com.zadtek.shopmall.activities.RedEnvelopActivity.TAB_3;

/**
 * Created by Zackv on 2018/4/11.
 */

public class AllOrderFragment extends AbsListFragment {
    private int currentTabIndex = TAB_1;

    private AllOrderAdapter allOrderAdapter;
    private List<OrderBean> data = new LinkedList<>();
    private int page = 1;
    private int pagesize = 10;

    public AllOrderFragment(){

    }

    public static AllOrderFragment Instance(int tabIndex){
        AllOrderFragment instance = new AllOrderFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("tabIndex",tabIndex);
        instance.setArguments(bundle);
        return instance;
    }
    @Override
    protected void firstRequestData() {
        Bundle bundle = getArguments();
        if(bundle!=null){
            currentTabIndex = bundle.getInt("tabIndex");
        }
        switch (currentTabIndex){
            case TAB_1:
                loadDataTab1();
                break;
            case TAB_2:
                loadDataTab2();
                break;
            case TAB_3:
                loadDataTab3();
                break;
        }

    }

    private void loadDataTab3() {
        Map<String, Object> param = NetTool.newParams();
        param.put("page",page);
        param.put("pagesize",pagesize);
        param.put("shipping_status",0);
        NetTool.getApi().getorders(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<List<OrderBean>>>(this) {
                    @Override
                    public void onData(BaseResponse<List<OrderBean>> value) {
                        if(value.getCode() == 0 && value.getData()!=null){
                            if(value.getData().size()!=0){
                                data.addAll(value.getData());
                                allOrderAdapter.loadMoreComplete();
                                page++;
                            }else{
                                allOrderAdapter.loadMoreEnd();

                            }
                        }
                    }
                });
    }

    private void loadDataTab2() {
        Map<String, Object> param = NetTool.newParams();
        param.put("page",page);
        param.put("pagesize",pagesize);
        param.put("pay_status",0);
        NetTool.getApi().getorders(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<List<OrderBean>>>(this) {
                    @Override
                    public void onData(BaseResponse<List<OrderBean>> value) {
                        if(value.getCode() == 0 && value.getData()!=null){
                            if(value.getData().size()!=0){
                                data.addAll(value.getData());
                                allOrderAdapter.loadMoreComplete();
                                page++;
                            }else{
                                allOrderAdapter.loadMoreEnd();

                            }
                        }
                    }
                });
    }

    private void loadDataTab1() {
        //Map<String, Object> param = NetTool.newParams();
        final Map<String, Object> param = new ArrayMap<String, Object>();
        param.put("uid", 75);
        param.put("token", Const.token);
        param.put("page",page);
        param.put("pagesize",pagesize);
        NetTool.getApi().getorders(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<List<OrderBean>>>(this) {
                    @Override
                    public void onData(BaseResponse<List<OrderBean>> value) {
                        if(value.getCode() == 0 && value.getData()!=null){
                            if(value.getData().size()!=0){
                                data.addAll(value.getData());
                                allOrderAdapter.loadMoreComplete();
                                page++;
                            }else{
                                allOrderAdapter.loadMoreEnd();
                            }
                        }
                    }
                });
    }

    @Override
    protected void loadMore() {
            switch (currentTabIndex){
                case TAB_1:
                    loadDataTab1();
                    break;
                case TAB_2:
                    loadDataTab2();
                    break;
                case TAB_3:
                    loadDataTab3();
                    break;
            }
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        allOrderAdapter = new AllOrderAdapter(R.layout.order_item,data);
        allOrderAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.tv_btn_cancel:
                        ToastUtil.showToast("btn_cancel");
                        break;
                    case R.id.tv_btn_show:
                        ToastUtil.showToast("btn_show");
                        break;
                    case R.id.tv_btn_apply:
                        ToastUtil.showToast("btn_apply");
                        break;
                }
            }
        });
        return allOrderAdapter;
    }

    @Override
    protected void OnItemClick(int position) {
        startActivity(new Intent(mActivity, OrderActivity.class).putExtra("order_sn",data.get(position).getOrder_sn()));
    }

    @Override
    protected void OnRefresh() {
        //data.clear();
        //allOrderAdapter.notifyDataSetChanged();
        setRefreshing(false);
    }
    private class AllOrderAdapter extends BaseQuickAdapter<OrderBean,BaseViewHolder>{

        public AllOrderAdapter(int layoutResId, @Nullable List<OrderBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(final BaseViewHolder helper, OrderBean item) {
            helper.addOnClickListener(R.id.tv_btn_cancel);
            helper.addOnClickListener(R.id.tv_btn_apply);
            helper.addOnClickListener(R.id.tv_btn_show);
            helper.setText(R.id.tv_shop_name,item.getShop_name());
            helper.setText(R.id.tv_order_id,"订单号："+item.getOrder_sn());
            helper.setText(R.id.tv_order_time,item.getAdd_time());
            helper.setText(R.id.tv_product_num,"共"+item.getCount()+"款");
            helper.setText(R.id.tv_product_num,"共"+item.getCount()+"款");
            helper.setText(R.id.tv_order_count,"共"+item.getCount()+"款商品  合计：");
            helper.setText(R.id.tv_order_price,"￥"+item.getGoods_amount());
            String paystatus = "";
            if(item.getPay_status() == 0){
                paystatus += "未付款";
                helper.getView(R.id.tv_btn_show).setVisibility(View.VISIBLE);
                helper.getView(R.id.tv_btn_apply).setVisibility(View.GONE);
                helper.getView(R.id.tv_btn_cancel).setVisibility(View.VISIBLE);
            }else if(item.getPay_status() == 2){
                paystatus += "已付款";
                helper.getView(R.id.tv_btn_show).setVisibility(View.VISIBLE);
                helper.getView(R.id.tv_btn_apply).setVisibility(View.GONE);
                helper.getView(R.id.tv_btn_cancel).setVisibility(View.VISIBLE);
            }

            if(item.getOrder_status() == 0){
                paystatus+=",未确认";
            }else if(item.getOrder_status() == 1){
                paystatus+=",已确认";

            }else if(item.getOrder_status() == 2){
                paystatus+=",已取消";

            }else if(item.getOrder_status() == 3){
                paystatus+=",无效订单";
            }else if(item.getOrder_status() == 4){
                //paystatus+=",正在退货";
            }
            if(item.getShipping_status() == 0){
                paystatus+=",未发货";
            }else if(item.getShipping_status() == 1){
                paystatus+=",已发货";

            }else if(item.getShipping_status() == 2){
                paystatus+=",已收货";

            }else if(item.getShipping_status() == 3){
                paystatus+=",退货中";
            }
            helper.setText(R.id.tv_order_state,paystatus);
            NoScrollView gv = helper.getView(R.id.gv_product_list);
            gv.setAdapter(new MyAdapter<OrderProudctBean>(item.getGoods_img(),R.layout.image_view) {
                @Override
                public void bindView(ViewHolder holder, OrderProudctBean obj) {
                    Glide.with(mActivity).load(obj.getImg()).placeholder(R.drawable.placeholder).into((ImageView) holder.getView(R.id.iv_img));
                }
            });
        }
    }
}
